<?php 
//TODO Tengo que filtrar la lista de productos en el form remisefournisseur cuando se crea el formulario.

?>
<?php use_javascript('/js/json_parse.js') ?>
<script type="text/javascript">
function getProduits(){
	idFournisseur = document.getElementById('remise_fournisseur_id').value;
	var xhr = getXmlHttpRequest2();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200)
			{
			var textCandidates = xhr.responseText;
			var textCandidatesData = json_parse(textCandidates);
			refreshSelect('remise_produit_id', textCandidatesData['produits'], null, textCandidatesData['produit']);
			
		}
	};
	xhr.open("POST","<?php print url_for('ajax/getProduitsFromFournisseur'); ?>",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
	xhr.send("fournisseur_id="+idFournisseur);
}

function getDpt(){
	var cp = document.getElementById('client_codpos').value;
	var xhr = getXmlHttpRequest2();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200)
			{
			var idDpt = xhr.responseText;
			document.getElementById('client_departement_id').value = idDpt;
		}
	};
	xhr.open("POST","<?php print url_for('clients/getDptFromCP'); ?>",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
	xhr.send("cp="+cp);		
}	

function insertRemises(){
	var id_client = document.getElementById('client_id').value;
	var checkboxes = $("#Formclient input:checkbox");
	var ar = new Array();
	var i = 0;
	for (i; i<checkboxes.length; i++) { 
		var pro = { 'id':checkboxes[i].value, 
					'checked':checkboxes[i].checked};
			//new Array(checkboxes[i].value,checkboxes[i].checked);
		createRemises(checkboxes[i].value, checkboxes[i].checked);
		ar.push(pro);
	}
	refreshItemList('remises');
	
}

function createRemises(idfour, xcreate){
	var xhr = getXmlHttpRequest2();
	xhr.open("POST","<?php print url_for('clients/getProduitsFromFournisseur'); ?>",false);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
	xhr.send('id=' + idfour);
	produits = json_parse(xhr.responseText);
	for(var i = 0; i < produits.length; i++){
		var ligne = remiseExists(idfour, produits[i].id);
		if(xcreate){
			if(ligne < 0){
				addRemise(idfour, produits[i].id);
			}
		}else{
			
		}
	}
}

//Retourne le numero d'item qui correspond a ce produit et ce fournisseur

function remiseExists(idfournisseur, idproduit){
	fieldname = 'remises';
	if(document.getElementById('q_list_remises')){
		numitems = parseInt(document.getElementById('q_list_remises').value);
		//list = $("[id^='client_remises_']"); //Starts with
		for(i = 0; i < numitems; i++){
			if(document.getElementById('client_remises_' + i + '_fournisseur_id')){
				item_idfournisseur = document.getElementById('client_remises_' + i + '_fournisseur_id').value;
			}
			if(document.getElementById('client_remises_' + i + '_produit_id')){
				item_idproduit = document.getElementById('client_remises_' + i + '_produit_id').value;
			}
			if(item_idfournisseur==idfournisseur && item_idproduit == idproduit){
				return i;
			}
		}
	}
	return -1;
	//list = $("[id$='fournisseur_id']"); //ends with
}

function addRemise(idfournisseur, idproduit){
	fieldname = 'remises';
	num = items_q[fieldname];
	var xhr = getXmlHttpRequest2();
	xhr.open("POST","<?php print url_for('clients/addRemise'); ?>",false);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
	var id = <?php echo ($form->getObject()->isNew()?'0':$form->getObject()->getId())?>;
	xhr.send('id=' + id + '&num=' +num + '&fieldname=' + fieldname + '&ownerclass=client' + '&idfournisseur=' + idfournisseur + '&idproduit=' + idproduit);
	var idDiv = 'list_' + fieldname;
	var newDiv = document.createElement('div');
	newDiv.innerHTML = xhr.responseText;
	newDiv.setAttribute('id', 'form_' + items_q[fieldname]);
	newDiv.setAttribute('style', 'display:none;');
	document.getElementById(idDiv).appendChild(newDiv);
	var index = items_q[fieldname];
	items_q[fieldname] = items_q[fieldname] + 1;

	
}

function insertRemisesOld(){
	var id_client = document.getElementById('client_id').value;
	var checkboxes = $("#clientForm input:checkbox");
	var ar = new Array();
	var i = 0;
	for (i; i<checkboxes.length; i++) { 
		var pro = new Array(checkboxes[i].value,checkboxes[i].checked);
		ar.push(pro);
	}
	var str = array2json(ar);
	var xhr = getXmlHttpRequest2();
	xhr.open("POST","<?php print url_for('clients/insertRemises'); ?>",false);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("id_client=" + id_client + "&checkboxes=" + str);	
	var xhrremises = getXmlHttpRequest2();
	xhrremises.open("POST","<?php print url_for('clients/getViewItems'); ?>",false);
	xhrremises.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrremises.setRequestHeader('X_REQUESTED_WITH','XMLHttpRequest');
	xhrremises.send("ownerClass=client&ownerId=" + id_client + "&fieldName=remises&className=remise"); 
	var htmlTable = xhrremises.responseText;
	document.getElementById('list_remises').innerHTML = htmlTable;
	$("a.fancy").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'titleShow'		: false
	});
}

function validationEncours(){
	var cc = document.getElementById('client_categorieclient_id').value;
	var en = document.getElementById('client_encour').value;
	if((cc == 1) && (parseInt(en) == 0)){
		alert("Veuillez changer de statut");
	}
}


</script>